let home_box = document.getElementsByClassName("home_box")[0];
let xh_intro_box = document.getElementsByClassName("home_box")[0];
let xh_announce_box = document.getElementsByClassName("home_box")[0];
let xh_activity_box = document.getElementsByClassName("home_box")[0];
let director_box = document.getElementsByClassName("home_box")[0];
let vip_box = document.getElementsByClassName("home_box")[0];
let online_sub_box = document.getElementsByClassName("home_box")[0];
let institution_box = document.getElementsByClassName("home_box")[0];

let liList = document.getElementsByClassName("li_item");

for (let index = 0; index < liList.length; index++) {
    const element = liList[index];
    let name = element.name
    xh_intro_box.classList.remove("show")
    xh_announce_box.classList.remove("show")
    director_box.classList.remove("show")
    switch (name) {
        case '0':
            home_box.classList.add("show")
           
            break;
        case "1":
           
            xh_intro_box.classList.add("show")
            
            break;
        default:
            break;
    }
}



let contentList = getData()
let contentBox = document.getElementById("content_box");

// 获取父元素
let pcnavPer = document.getElementById('pc_nav_ul');
let monavPer = document.getElementById("mo_nav_ul");
let thisNva = 0;
let thisNvaObj = {};
let thisTraget = {};



// 事件代理：在父元素上添加事件监听器
pcnavPer.addEventListener('click', function (event) {
    var target = event.target;
    pcMoNavCheck(target, "pc");
});

monavPer.addEventListener('click', function (event) {
    var target = event.target;
    pcMoNavCheck(target, "mo");
});

function pcMoNavCheck(target, type) {
    if (type == "mo") {
        navCheck();
    }
   
    if (target.getAttribute('name') == thisNva) {
        return;
    } else {
        if (type == "pc") {
            if (thisTraget && thisTraget["classList"]) {
                 
                thisTraget.classList.remove("sel");
            }
            thisTraget = target;
            target.classList.add("sel");
        }
        navCheckHandle(target.getAttribute('name'));
    }
}

function navCheckHandle(nodeName) {
    thisNva = nodeName;
    removeStyles(thisNvaObj["cssUrl"]);
    // console.log(contentList)
    thisNvaObj = contentList[+thisNva] || {"html": "页面开发中。。。。。。。。", "cssUrl": ""};
    // console.log(thisNvaObj)
    importCss(thisNvaObj["cssUrl"])

    contentBox.innerHTML = thisNvaObj["html"];
}


function importCss(url) {

    if (url) {
        let cssLink = document.createElement('link');
        cssLink.setAttribute("rel", "stylesheet")
        cssLink.setAttribute("type", "text/css")
        cssLink.setAttribute("href", url)

        document.head.appendChild(cssLink);
    }
}
function removeStyles(file) {
    // console.log("将要删除的css：" + file)
    if (file) {
        var filename = file;
        var targetelement = "link";
        var targetattr = "href";
        var allsuspects = document.getElementsByTagName(targetelement);
        for (var i = allsuspects.length; i >= 0; i--) {
            if (allsuspects[i] && allsuspects[i].getAttribute(targetattr) != null && allsuspects[i].getAttribute(
                targetattr).indexOf(filename) != -1) {
                allsuspects[i].parentNode.removeChild(allsuspects[i])
            }
        }
    }
}


let navShow = false;
const navCheck = () => {
    navShow ? navShow = false : navShow = true;
    let monav = document.getElementsByClassName("mo_nav")[0];
    navShow ? monav.classList.add("hider") : monav.classList.remove("hider");

}
window.onload = function () {
    thisTraget = document.getElementsByClassName("sel")[0];
    navCheckHandle("0")
}



const totop = () => {
    window.scrollTo({
        top: 0,
        behavior: "smooth",
    });
};